package com.yifang.yibingproject.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifang.yibingproject.entity.IpInfoEntity;
import com.yifang.yibingproject.mapper.IpInfoMapper;
import com.yifang.yibingproject.service.IpInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class IpInfoServiceImpl extends ServiceImpl<IpInfoMapper, IpInfoEntity> implements IpInfoService {
    private static final Logger log = LoggerFactory.getLogger(com.yifang.yibingproject.service.impl.IpInfoServiceImpl.class);

    //聚合数据接口和APP密钥
    private static final String JH_IP_API = "http://apis.juhe.cn/ip/ipNew?key=ffdb7f0ade8bf5d0233f665403b08530&ip={ip}";

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public IpInfoEntity getIpInfo(String ip) {
        log.info("获取IP=" + ip + "地址信息 START");
        IpInfoEntity entity = getById(ip);
        if (entity == null) {
            entity = getIpInfoFromJvHeApi(ip);
            if (entity != null)
                saveOrUpdate(entity);//保存接口查询结果
        }
        log.info("获取IP=" + ip + "地址信息 END INFO -> " + entity);
        return entity;
    }


    private IpInfoEntity getIpInfoFromJvHeApi(String ip) {
        IpInfoEntity entity = null;
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity(JH_IP_API, Map.class, ip);
        if (HttpStatus.OK == responseEntity.getStatusCode()) {
            Map map = responseEntity.getBody();
            if (map != null && "200".equals(map.get("resultcode"))) {
                Map result = (Map) map.get("result");
                if (result != null) {
                    entity = new IpInfoEntity();
                    entity.setIp(ip);
                    if (result.get("Country") != null)
                        entity.setCountry((String) result.get("Country"));
                    if (result.get("Province") != null)
                        entity.setProvince((String) result.get("Province"));
                    if (result.get("City") != null)
                        entity.setCity((String) result.get("City"));
                    if (result.get("Isp") != null)
                        entity.setIsp((String) result.get("Isp"));
                    entity.setBlock(!"中国".equals(entity.getCountry()));
                    entity.setCreateTime(LocalDateTime.now());
                    entity.setAccess(1);
                }
            } else {
                log.info("调用聚合IP接口成功，但查询IP失败：" + JSONObject.toJSONString(map));
            }
        } else {
            log.info("调用聚合IP接口失败：code=" + responseEntity.getStatusCode() + ",value=" + responseEntity.getStatusCodeValue());
        }
        return entity;
    }
}
